<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>

        function sum(a, b) {
            // 给参数一个默认值  --- 如果调用函数的时候，没有传参数，就默认使用0这个值
            a = a || 0;
            b = b || 0;
            console.log(a + b);
        }

        sum();    // a=undefined ||0  a=0   0
        sum(2, 3);  //a=2||0    5


        // 随机验证码 , 默认为5位
        function randCode(n) {
            n = n || 5;
            var res = '';
            for (var i = 0; i < n; i++) {
                res += parseInt(Math.random() * 10);
            }
            return res;
        }

        console.log(randCode());


        // 短路赋值
        var a = 1 || 2;
        console.log(a);   // 1

        var b = 0 || 3;
        console.log(b);   // 3

        var c = 1 && 2;
        console.log(c);   // 2

        var d = 0 && 4;
        console.log(d);   // 0


    </script>

</body>

</html>